package im.yisme.view.ui;

import im.yisme.R;
import im.yisme.view.ui.util.YViewThread;
import android.R.integer;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.view.SurfaceHolder;

public class MainView extends YView {

	private Bitmap gameBmp, recordBmp, musicBmp, lyricsBmp;
    private Bitmap bgp,bgp1,bgp2,bgp3,bgp4,bgp5;
    private int index;
    private int count;
    private static final int NUM_OF_BGP=5;
	
	public MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initBitmap();
        setBackgroundResource(R.drawable.transparent_bgp);
	}
	
	public void setBGP(Bitmap bgp){
		this.bgp = bgp;
	}
	@Override
	protected void createThread() {
		// TODO Auto-generated method stub
		paintThread = new MainViewPaintThread(holder);
	}
	
	private void initBitmap(){
		gameBmp = BitmapFactory.decodeResource(getResources(), R.drawable.main_game);  
		recordBmp = BitmapFactory.decodeResource(getResources(), R.drawable.main_record);  
		musicBmp = BitmapFactory.decodeResource(getResources(), R.drawable.main_music);  
		lyricsBmp = BitmapFactory.decodeResource(getResources(), R.drawable.main_lyrics);  
		bgp1 = BitmapFactory.decodeResource(getResources(), R.drawable.bgp1);
		bgp2 = BitmapFactory.decodeResource(getResources(), R.drawable.bgp2);
		bgp3 = BitmapFactory.decodeResource(getResources(), R.drawable.bgp3);
		bgp4 = BitmapFactory.decodeResource(getResources(), R.drawable.bgp4);
		bgp5 = BitmapFactory.decodeResource(getResources(), R.drawable.bgp5);
		bgp = bgp1;
	}
	private Bitmap getBGP(int index){
		Bitmap bm = null;
		switch (index) {
		case 0:
			bm = bgp1;
			break;
		case 1:
			bm = bgp2;
			break;
		case 2:
			bm = bgp3;
			break;
		case 3:
			bm = bgp4;
			break;
		case 4:
			bm = bgp5;
			break;
		default:
			break;
		}
		
		return bm;
	}

	/**
	 * 绘图线程
	 * @author Monster
	 *
	 */
	class MainViewPaintThread extends YViewThread{

		public MainViewPaintThread(SurfaceHolder holder) {
			super(holder);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void paint(Canvas canvas) {
			// TODO Auto-generated method stub
			canvas.drawColor(Color.WHITE);
			
			Paint paint = new Paint();
			paint.setStrokeWidth(1);
			paint.setAntiAlias(true);
			
			
			if(count == 5){
				index = (index+1)%NUM_OF_BGP;
				bgp = getBGP(index);
				count = 0;
			}
			else {
				count++;
			}
			
			
			if(bgp != null)
			canvas.drawBitmap(bgp, 0, 0, paint);
			
			
			Resources r = getResources(); 
			float game_x = r.getDimension(R.dimen.main_game_button_x);
			float game_y = r.getDimension(R.dimen.main_game_button_y);
			float record_x = r.getDimension(R.dimen.main_record_button_x);
			float record_y = r.getDimension(R.dimen.main_record_button_y);
			float music_x = r.getDimension(R.dimen.main_music_button_x);
			float music_y = r.getDimension(R.dimen.main_music_button_y);
			float lyrics_x = r.getDimension(R.dimen.main_lyrics_button_x);
			float lyrics_y = r.getDimension(R.dimen.main_lyrics_button_y);
			
			canvas.drawBitmap(gameBmp, game_x, game_y, paint);
			canvas.drawBitmap(recordBmp, record_x, record_y, paint);
			canvas.drawBitmap(musicBmp, music_x, music_y, paint);
			canvas.drawBitmap(lyricsBmp, lyrics_x, lyrics_y, paint);
		}
		
	}
}
